package com.amoyt.project.controller.admin;


import com.amoyt.project.annotations.BusinessLog;
import com.amoyt.project.annotations.ModuleLog;
import com.amoyt.project.domain.dto.insert.AssistanceAddDto;
import com.amoyt.project.domain.dto.query.AssistancePageQuery;
import com.amoyt.project.domain.dto.update.AssistanceUpdateDto;
import com.amoyt.project.domain.res.Result;
import com.amoyt.project.exception.BusinessException;
import com.amoyt.project.exception.enums.BusinessExceptionEnum;
import com.amoyt.project.service.admin.AssistanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 帮扶措施表 前端控制器
 * </p>
 *
 * @author AmoyT
 * @since 2025-08-21
 */
@RestController
@RequestMapping("/admin/assistance")
@Tag(name = "帮扶措施接口",description = "帮扶措施接口")
@ModuleLog(module = "帮扶措施操作模块")
public class AssistanceController {
    @Autowired
    private AssistanceService assistanceService;

    /**
     * 帮扶措施列表
     * @param query
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "帮扶措施列表",method = "GET")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result list(AssistancePageQuery query) {
        if (query == null || query.getPage() == null || query.getPageSize() == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return assistanceService.list(query);
    }

    /**
     * 更新帮扶措施
     * @param assistanceUpdateDto
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新帮扶措施",method = "PUT")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "更新帮扶措施",type = "2")
    public Result update(@RequestBody AssistanceUpdateDto assistanceUpdateDto) {
        if (assistanceUpdateDto == null || assistanceUpdateDto.getMeasureId() == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return assistanceService.update(assistanceUpdateDto);
    }

    /**
     * 添加帮扶措施
     * @param assistanceAddDto
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "添加帮扶措施",method = "POST")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "添加帮扶措施",type = "1")
    public Result add(@RequestBody AssistanceAddDto assistanceAddDto) {
        if (assistanceAddDto == null) {
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return assistanceService.add(assistanceAddDto);
    }

    /**
     * 删除帮扶措施
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    @Operation(summary = "删除帮扶措施",method = "DELETE")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin')")
    @BusinessLog(business = "删除帮扶措施",type = "3")
    public Result delete(@PathVariable List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return assistanceService.delete(ids);
    }
}

